package com.kyzh.core.pager.weal.serverlist

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.ServerV3
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.BiaoqianAdapter
import com.kyzh.core.databinding.ItemImagesBinding
import com.kyzh.core.databinding.ItemServerV3Binding
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startGameDetailActivity
import kotlinx.android.synthetic.main.activity_server.*

class ServerActivity : BaseActivity(){


    private var p =1
    private var max =1
    private val beans = arrayListOf<ServerV3>()
    private val adapter = Adapter(R.layout.item_server_v3,beans)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_server)
        initView()
    }


    private fun initView() {
/*
        val titles = mutableListOf("即将开服","已经开服")
        viewpager.adapter = ServerTabAdapter(this@ServerActivity,titles)
        viewpager.currentItem = 0
        tab.setupWithViewPager(viewpager)*/
        refresh(0)
        rvServer.adapter = adapter
        srlRoot.setOnRefreshListener {
            refresh(0)
        }
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            getData(0)
            adapter.loadMoreModule.loadMoreComplete()
        }
    }




    fun getData(type:Int){
        GameRequest.server(p,type){  p, max_p,data->
            this@ServerActivity.p = p
            this@ServerActivity.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
            if(p>=max_p){
                adapter.loadMoreModule.loadMoreEnd()
            }
        }
    }
    fun refresh(type:Int){
        GameRequest.server(1,type){  p, max_p,data->
            this@ServerActivity.p = p
            this@ServerActivity.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
            srlRoot.isRefreshing = false
            if(p>=max_p){
                adapter.loadMoreModule.loadMoreEnd()
            }
        }
    }
    private inner class Adapter(layout: Int, beans: ArrayList<ServerV3>): BaseQuickAdapter<ServerV3, BaseDataBindingHolder<ItemServerV3Binding>>(layout,beans), LoadMoreModule {

        override fun convert(holder: BaseDataBindingHolder<ItemServerV3Binding>, item: ServerV3) {
            holder.dataBinding?.let {
                it.data = item
                it.bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                it.bq.adapter = BiaoqianAdapter( item.biaoqian)
                holder.itemView.setOnClickListener {
                    context.startGameDetailActivity(item.game_id)
                }
                if(item.zhekou.isNullOrEmpty()){
                    it.btDiscount.setVisibility(false)
                }else{
                    if(!item.zhekou.contains("折")){
                        it.btDiscount.text = item.zhekou+"折"
                    }else{
                        it.btDiscount.text = item.zhekou
                    }
                    it.btDiscount.setVisibility(true)
                    if(item.zhekou.contains("10")){
                        it.btDiscount.setVisibility(false)
                    }
                }

                it.rvImages.layoutManager = object : GridLayoutManager(this@ServerActivity,3){
                    override fun canScrollHorizontally(): Boolean {
                        return false
                    }

                    override fun canScrollVertically(): Boolean {
                        return false
                    }
                }
                it.rvImages.adapter = ImageAdapter(R.layout.item_images,item.imgs)
                it.tvTime.text = "${item.time} ${item.server_name}"
            }
        }
    }

    private inner class ImageAdapter(layout:Int,beans:ArrayList<String>):BaseQuickAdapter<String,BaseDataBindingHolder<ItemImagesBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemImagesBinding>, item: String) {
            holder.dataBinding?.let {
                it.data = item
            }
        }
    }
}
